-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
opt: don't add reordered join with extra filters to original memo group #88779
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job tracking this down!
Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball)
-- commits
line 2 at r1:
nit: consider framing this as something other than "pushdown" - see my other comment
-- commits
line 15 at r1:
I think an example like you gave in our session today would be helpful - especially if we come back and read this months or years later. Also, I think it'd be helpful to call out explicitly that this happens when a join with a synthesized filter gets added to an existing memo group that has an expression where the synthesized filter was not created (pushed-down). And that this is only possible when that original expression in the group is chosen as part of the optimal plan, which is rare.
pkg/sql/opt/xform/join_order_builder.go
line 367 at r1 (raw file):
// Ensure that if any of the joins did not successfully push filters down as // far as possible, all joins apart from the root join will be rebuilt. jb.ensurePushdown()
I think framing this as a problem with "pushdown" is a little confusing, even if that is the root cause. It seems like what we are actually checking is that all edges did not synthesize filters not in the original join. So maybe there is a way to rename this and reword the comments here and below? I think you can still mention the pushdown as a cause for when this would happen, but in theory there could be other reasons we end up synthesizing a filter that wasn't in the original join.
pkg/sql/opt/xform/join_order_builder.go
line 395 at r1 (raw file):
e := &jb.edges[i] isFilter := len(e.filters) > 0 && e.filters[0].ScalarProps().OuterCols.Contains(3) && e.filters[0].ScalarProps().OuterCols.Contains(9) _ = isFilter
nit: isFilter
looks like leftover code from debugging
pkg/sql/opt/xform/join_order_builder.go
line 695 at r1 (raw file):
// This inferred filter was not pushed down as far as possible. All joins // apart from the root will have to be rebuilt. jb.rebuildAllJoins = true
Is this part needed, or is it an optimization to avoid some of the work in ensurePushdown if we can prove we need to rebuild all joins here?
pkg/sql/opt/xform/join_order_builder.go
line 1406 at r1 (raw file):
// was already present in the original join tree. If so, enumerating this join // would be redundant, so it should be skipped. func (e *edge) joinIsRedundant(jb *JoinOrderBuilder, s1, s2 vertexSet) bool {
nit: might be simpler to make this a method of JoinOrderBuilder
instead of edge
now.
nit: Add a note explaining that if rebuildAllJoins
is true then none of new joins are redundant with original joins.
pkg/sql/opt/xform/testdata/rules/join_order
line 2918 at r1 (raw file):
(t3.a) = (t4.b) AND (t2.b) = (t4.a) AND (t2.a) = (t4.a);
nit: tabs to spaces
bors r+ |
👎 Rejected by too few approved reviews |
Oops, wrong PR. |
6bdb468
to
8d82367
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @mgartner)
Previously, mgartner (Marcus Gartner) wrote…
nit: consider framing this as something other than "pushdown" - see my other comment
Done, let me know what you think.
Previously, mgartner (Marcus Gartner) wrote…
I think an example like you gave in our session today would be helpful - especially if we come back and read this months or years later. Also, I think it'd be helpful to call out explicitly that this happens when a join with a synthesized filter gets added to an existing memo group that has an expression where the synthesized filter was not created (pushed-down). And that this is only possible when that original expression in the group is chosen as part of the optimal plan, which is rare.
Added an example. Done.
pkg/sql/opt/xform/join_order_builder.go
line 367 at r1 (raw file):
Previously, mgartner (Marcus Gartner) wrote…
I think framing this as a problem with "pushdown" is a little confusing, even if that is the root cause. It seems like what we are actually checking is that all edges did not synthesize filters not in the original join. So maybe there is a way to rename this and reword the comments here and below? I think you can still mention the pushdown as a cause for when this would happen, but in theory there could be other reasons we end up synthesizing a filter that wasn't in the original join.
I've changed the name to validateEdges
and expanded the comments. LMK what you think.
pkg/sql/opt/xform/join_order_builder.go
line 395 at r1 (raw file):
Previously, mgartner (Marcus Gartner) wrote…
nit:
isFilter
looks like leftover code from debugging
Oops, good catch. Done.
pkg/sql/opt/xform/join_order_builder.go
line 695 at r1 (raw file):
Previously, mgartner (Marcus Gartner) wrote…
Is this part needed, or is it an optimization to avoid some of the work in ensurePushdown if we can prove we need to rebuild all joins here?
It's necessary since we set the op for the edge to the original join to which the filter would have been pushed if it existed in the original join tree, so the applicability check would always succeed. I've added a comment.
pkg/sql/opt/xform/join_order_builder.go
line 1406 at r1 (raw file):
Previously, mgartner (Marcus Gartner) wrote…
nit: might be simpler to make this a method of
JoinOrderBuilder
instead ofedge
now.nit: Add a note explaining that if
rebuildAllJoins
is true then none of new joins are redundant with original joins.
Done.
pkg/sql/opt/xform/testdata/rules/join_order
line 2918 at r1 (raw file):
Previously, mgartner (Marcus Gartner) wrote…
nit: tabs to spaces
Done.
TFTR! I know it's a bit tough to wade through this code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
Reviewed 1 of 2 files at r2.
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball and @mgartner)
Previously, DrewKimball (Drew Kimball) wrote…
Added an example. Done.
Nice!
-- commits
line 16 at r2:
nit: "and so wouldn't add" -> I think you're missing a word or two there?
-- commits
line 38 at r2:
nit: "correct" is confusing given that the plan is incorrect, maybe "is essentially the problem fixed by this commit" or something
pkg/sql/opt/xform/join_order_builder.go
line 695 at r1 (raw file):
Previously, DrewKimball (Drew Kimball) wrote…
It's necessary since we set the op for the edge to the original join to which the filter would have been pushed if it existed in the original join tree, so the applicability check would always succeed. I've added a comment.
I see. So it seems like this is required to fix this exact bug, while validateEdges
provides extra safety for other cases that we aren't currently aware of. is that correct?
The `JoinOrderBuilder` builds reordered join plans from the bottom up. It expects filters to be pushed down as far as possible at each step, and that transitive closure has been calculated over Inner Join equality filters (e.g. `a=b` and `b=c` => `a=c`). It also reuses the original matched joins when possible to avoid duplicate work by adding to the original memo groups. This could previously cause filters to be dropped in the case when the original join tree did not compute transitive closure and push filters down as far as possible. More specifically, the `JoinOrderBuilder` could add new reordered joins with new filters synthesized and pushed down as far as possible to an original memo group that didn't have one of those filters. Subsequent joins would then expect the filter to be part of the memo group, and so it wouldn't be added later on in the plan. In the rare case when the expression without the filter was chosen, this could manifest as a dropped filter in the final plan. This was rare because dropping a filter usually does not produce a lower-cost plan. As an example, take this original join tree: ``` (xy join ab on true) join uv on x = u and a = u; ``` Here it is possible to sythesize and push down a `x = a` filter, and so the `JoinOrderBuilder` would do this and add it to the group: ``` group (xy join ab on true), (xy join ab on x = a) ``` Later joins would use this group as an input, an expect the `x = a` filter to be present. If costing happened to choose the first expression in the group, we would end up choosing a plan like this: ``` (xy join ab on true) join uv on x = u ``` Where the `a = u` filter isn't included in the top-level join because it would be redundant to add it when `x = u` and `x = a` are already present. This is a bit of a simplification, but is essentially the problem fixed by this commit. This commit adds a check to the `JoinOrderBuilder` to identify cases where filters (including ones sythesized from the transitive closure) weren't pushed all the way down in the original join tree. When this is true, none of the originally matched joins can be reused when reordered joins are built except for the root join. This solution may perform some duplicate work when filters aren't pushed down, but it shouldn't matter because this case is rare (and should be avoided whenever possible). Fixes cockroachdb#88659 Release note (bug fix): Fixed a bug introduced in 20.2 that could cause filters to be dropped from a query plan with many joins in rare cases.
8d82367
to
73e8b28
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @mgartner)
Previously, mgartner (Marcus Gartner) wrote…
nit: "and so wouldn't add" -> I think you're missing a word or two there?
Done.
Previously, mgartner (Marcus Gartner) wrote…
nit: "correct" is confusing given that the plan is incorrect, maybe "is essentially the problem fixed by this commit" or something
Done.
pkg/sql/opt/xform/join_order_builder.go
line 695 at r1 (raw file):
Previously, mgartner (Marcus Gartner) wrote…
I see. So it seems like this is required to fix this exact bug, while
validateEdges
provides extra safety for other cases that we aren't currently aware of. is that correct?
Exactly.
bors r+ |
Build failed: |
bors r+ |
Build succeeded: |
Encountered an error creating backports. Some common things that can go wrong:
You might need to create your backport manually using the backport tool. error creating merge commit from 73e8b28 to blathers/backport-release-21.2-88779: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict [] you may need to manually resolve merge conflicts with the backport tool. Backport to branch 21.2.x failed. See errors above. error creating merge commit from 73e8b28 to blathers/backport-release-22.1-88779: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict [] you may need to manually resolve merge conflicts with the backport tool. Backport to branch 22.1.x failed. See errors above. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
blathers backport 22.2.0 |
blathers backport 22.1 |
Encountered an error creating backports. Some common things that can go wrong:
You might need to create your backport manually using the backport tool. error creating merge commit from 73e8b28 to blathers/backport-release-22.1-88779: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict [] you may need to manually resolve merge conflicts with the backport tool. Backport to branch 22.1 failed. See errors above. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
The
JoinOrderBuilder
builds reordered join plans from the bottom up.It expects filters to be pushed down as far as possible at each step, and
that transitive closure has been calculated over Inner Join equality filters
(e.g.
a=b
andb=c
=>a=c
). It also reuses the original matched joinswhen possible to avoid duplicate work by adding to the original memo groups.
This could previously cause filters to be dropped in the case when the
original join tree did not compute transitive closure and push filters down
as far as possible. More specifically, the
JoinOrderBuilder
could add newreordered joins with new filters synthesized and pushed down as far as possible
to an original memo group that didn't have one of those filters. Subsequent
joins would then expect the filter to be part of the memo group, and so it
wouldn't be added later on in the plan. In the rare case when the expression
without the filter was chosen, this could manifest as a dropped filter in the
final plan. This was rare because dropping a filter usually does not produce
a lower-cost plan.
As an example, take this original join tree:
Here it is possible to sythesize and push down a
x = a
filter, and so theJoinOrderBuilder
would do this and add it to the group:Later joins would use this group as an input, an expect the
x = a
filter tobe present. If costing happened to choose the first expression in the group,
we would end up choosing a plan like this:
Where the
a = u
filter isn't included in the top-level join because itwould be redundant to add it when
x = u
andx = a
are already present.This is a bit of a simplification, but is essentially the problem fixed
by this commit.
This commit adds a check to the
JoinOrderBuilder
to identify cases wherefilters (including ones sythesized from the transitive closure) weren't
pushed all the way down in the original join tree. When this is true, none
of the originally matched joins can be reused when reordered joins are
built except for the root join. This solution may perform some duplicate
work when filters aren't pushed down, but it shouldn't matter because this
case is rare (and should be avoided whenever possible).
Fixes #88659
Release note (bug fix): Fixed a bug introduced in 20.2 that could cause
filters to be dropped from a query plan with many joins in rare cases.